<?php

declare(strict_types=1);

namespace app\common\service\crm;

use app\common\service\sys\DictService;
use app\common\traits\ServiceTrait;
use app\common\dao\crm\ContactsDao;
use app\common\dao\crm\CustomerDao;
use think\facade\Cache;

/**
 * 联系人表 服务业务层(Service)
 * @author 吕玉冰
 * @date  2022-07-21
 * @copyright ©2022 泉州市盛筑信息科技有限公司 版权所有
 */
class ContactsService
{
    protected $dao = null;

    public function __construct()
    {
        $this->dao = new ContactsDao();
        $this->cdao = new CustomerDao();
        $this->dictDao = new DictService();
    }

    use ServiceTrait;

    /**
     * lvyubing
     * @param $data
     * @return array
     */
    public function _list_where($data)
    {
        $where = [];
        if (isset($data['customer_id']) && $data['customer_id'] != 0) {
            $where[] = ['customer_id', '=', $data['customer_id']];
        }
        $data['where'] = $where;
        return [
            'where' => isset($data['where']) ? $data['where'] : [],
            'order' => isset($data['order']) ? $data['order'] : [],
            'field' => isset($data['field']) ? $data['field'] : ['*'],
        ];
    }

    public function getCustomerInfo($id)
    {
        $customer_list = Cache::get('crm_customer_list');
        if (isset($customer_list[$id]) && !empty($customer_list[$id])) {
            return $customer_list[$id];
        } else {
            $list = $this->cdao->getInfoById($id);
            $infos = Cache::get('crm_customer_list');
            $infos[$id] = $list['name'];
            Cache::set("crm_customer_list", $infos);
            return $infos[$id];
        }
    }

    /**
     * 格式化数据
     */
    public function _list_format($data)
    {
        $where = [];
        $where[] = ["code", "=", "custome_role"];
        $info = $this->dictDao->getInfoByWhere($where);
        $where = [];
        $where[] = ["pid", "=", $info['id']];
        $list = $this->dictDao->getListAll($where);
        $temp = [];
        foreach ($list as $key => $value) {
            $temp[$value['value']] = $value['name'];
        }

        foreach ($data as $key => $value) {
            $data[$key]['role_name'] = $temp[$value['role']];
        }
        return $data;
    }

    public function _out_form_filter(&$data)
    {
        $where = [];
        $where[] = ["code", "=", "custome_role"];
        $info = $this->dictDao->getInfoByWhere($where);
        $where = [];
        $where[] = ["pid", "=", $info['id']];
        $list = $this->dictDao->getListAll($where);
        $temp = [];
        foreach ($list as $key => $value) {
            $temp[$value['value']] = $value['name'];
        }
        $data['role_name'] = $temp[$data['role']];
    }

    public function _list_with()
    {
        return [];
    }

    /**
     * lvyubing
     */
    public function auto($data)
    {
        $saveDate = [];
        $saveDate["name"] = $data['name'];
        $saveDate["customer_id"] = $data['customer_id'];
        $this->dao->add($saveDate);
    }

}
    